# -*- coding:utf-8 -*-
from database.db_handler import MysqlHander
from database.db_business import DbBusiness
from common.my_http import MyHttp
from common.my_baidu import MyBaidu
from common.my_time import MyTime
from common.macro_data import MacroData
import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import os


'''
网络数据
'''


class BaiduGuojiguanxiMaoyi2:
    def __init__(self):
        self.macro = MacroData()
        self.business = DbBusiness()

    def get_data(self):
        end_date = MyTime.forward_relative_date(3)
        diqu = self.business.query_diqu()
        check_title1 = ["进口","出口","进出口"]
        check_title1 = ["增长","减少","限制","暂停","清单","受限","冻结"]
        for d in diqu:
            next_page = True
            words = [d, "进出口"]
            for i in range(1,5):
                #time.sleep(1)
                if next_page == False:
                    break
                url = MyBaidu.get_url_order_by_time(words, i)
                try:
                    proxy = self.business.query_proxy()
                    res = MyBaidu.get_baidu_data_by_proxy(url, proxy)
                    for r in res:
                        data_date = MyBaidu.calc_date(r["date"])
                        if data_date < end_date:
                            next_page = False
                            break
                        if MyBaidu.check_site(r["site"]) == False:
                            continue
                        if MyBaidu.check_title(r["title"], [[d], check_title1, check_title2]) == False:
                            continue
                        data_title = r["title"]
                        data_site = r["site"]
                        data_url = r["url"]
                        
                        print(r["date"] + data_date)
                        self.macro.add_macro_data(data_date, 304, data_title, data_url, data_site)
                except Exception as e:
                    i = i - 1
                    pass

def guojiguanxi_maoyi2_api():
    d = BaiduGuojiguanxiMaoyi2()
    d.get_data()

if __name__ == '__main__':
    guojiguanxi_maoyi2_api()
